//
//  DynamicViewController.m
//  TouTiao
//
//  Created by 杜倩 on 16/9/19.
//  Copyright © 2016年 Match. All rights reserved.
//

#import "DynamicViewController.h"
#import <MJRefresh.h>
#import "WebViewController.h"
#import "SendViewController.h"
    
@interface DynamicViewController ()
    
@property(nonatomic,strong)UIButton *Titlebtn;//动态标题
    
@property(nonatomic,strong)UIButton *button;//返回按钮

@end
    
@implementation DynamicViewController
    
- (void)viewDidLoad {
        [super viewDidLoad];
    
//    self.navigationController.navigationBar setBarTintColor:[UIColor ]
    
    //隐藏分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        [self addTitle];//添加标题
        [self addBackBtn];//设置返回按钮
        [self addRight];//设置右边按钮
        [self sendDynamic];//设置发送消息按钮
        
    }
    
#pragma mark 添加标题--动态
-(void)addTitle{
        _Titlebtn = [[UIButton alloc] initWithFrame:CGRectMake(120, 2, 135, 40)];
        
        [_Titlebtn setTitle:@"动态" forState:UIControlStateNormal];
        [_Titlebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_Titlebtn setImage:[UIImage imageNamed:@"titlebar_refresh_small_night"] forState:UIControlStateNormal];
        self.navigationItem.titleView = _Titlebtn;
        [_Titlebtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    }
    
#pragma mark 进去刷新
-(void)refresh{
    //[self.tableView reloadData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self performSelector:@selector(headerRefresh) withObject:nil afterDelay:0.2];
    }];
    UILabel *la = [[UILabel alloc]  initWithFrame:CGRectMake(0,64, [UIScreen mainScreen].bounds.size.width, 20)];
    la.text = @"继续发送动态";
    la.textAlignment = NSTextAlignmentCenter;
    la.backgroundColor = [UIColor blueColor];
    [self.view addSubview:la];
    [self.tableView.mj_header beginRefreshing];//进来就刷新
    
}

-(void)headerRefresh{

   [self.tableView.mj_header endRefreshing];

}
    
#pragma mark 设置返回按钮
-(void)addBackBtn{
    _button=[UIButton buttonWithType:UIButtonTypeCustom];
        //设置按钮内边距
    _button.contentEdgeInsets=UIEdgeInsetsMake(0, -5, 0, 0);//返回按钮箭头距离边缘的大小
    
    [_button setImage:[UIImage imageNamed:@"leftbackicon_sdk_login@2x.png"] forState:UIControlStateNormal];
    [_button setImage:[UIImage imageNamed:@"leftbackicon_white_titlebar@2x"] forState:UIControlStateHighlighted];
        
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:_button];
        [_button sizeToFit];//自动适应大小
        [_button addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;//清空手势，实现拖拽
    self.navigationItem.leftBarButtonItem=item;
}



#pragma Mark 返回按钮的单击事件
-(void)goToBack{
    self.tabBarController.tabBar.hidden=NO;//返回显示标签栏
    [self.navigationController popToRootViewControllerAnimated:YES];//返回上一级控制器
    [_Titlebtn removeFromSuperview];//移除所有子控件
    
}

#pragma mark 右边的按钮
-(void)addRight{
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(340, 0, 30, 40)];
    [right setImage:[UIImage imageNamed:@"follow_title_profile"] forState:UIControlStateNormal];
    [right setImage:[UIImage imageNamed:@"follow_title_profile_night"] forState:UIControlStateSelected];
    
    UIBarButtonItem *item1=[[UIBarButtonItem alloc] initWithCustomView:right];
    
    [right addTarget:self action:@selector(RightTap) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem =  item1;
}
#pragma mark 右边按钮的单击事件
-(void)RightTap{
    WebViewController *web = [[WebViewController alloc] init];
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark 发送动态按钮
-(void)sendDynamic{
    UIButton *send = [[UIButton alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-90,[UIScreen mainScreen].bounds.size.width, 40)];
    send.backgroundColor = [UIColor blackColor];
    [send setTitle:@"发动态" forState:UIControlStateNormal];
    [send setImage:[UIImage imageNamed:@"writeicon_details_dynamic_night@3x.png"] forState:UIControlStateNormal];
    [send addTarget:self action:@selector(Run) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:send];
    
}

#pragma mark 发送动态按钮的单击事件
-(void)Run{
    SendViewController *sendView = [[SendViewController alloc] init];
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:sendView];
    //利用模态改变界面弹出的方向
    [self presentViewController:na animated:YES completion:nil];
}

@end
